﻿using Module_Debug.Hardware;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Module_Debug.Visionox_Gamma
{
    public partial class NT37700_Gamma : Form
    {
        CaSDK CaSDKObj = new CaSDK();
        CRC16 CRCData = new CRC16();
        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke 
        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过
        private double[,] D_lums;//目标值-单色
        private double[] D_lum;//目标值-白色
        private int index = 0;
        private int flag1 = 1; //lum
        private int flag2 = 1; //x
        private int flag3 = 1; //y
        private byte[] Send_buf = new byte[1];
        private byte[] Rec_tmp = new byte[1];
        //创建类型为Person的对象集合
        List<Setting.Gamma_Property> Diven_Gamma = new List<Setting.Gamma_Property>();
        List<Setting.Offset_Property> Diven_Offset = new List<Setting.Offset_Property>();
        string Path = System.AppDomain.CurrentDomain.BaseDirectory + "NT37700C" + ".txt";

        private static bool Gamma_adjust_timeout_flag = false;
        //Gamma绑点定义
        private int[] Parttern_NT37700C = new int[34] { 255, 247, 239, 231, 223, 215, 207, 199, 191, 183, 175, 159, 143, 127, 111, 95, 79, 63, 55, 47, 39, 31, 23, 19, 17, 15, 13, 11, 9, 7, 5, 3, 1, 0 };

        //监听NT37700挂载
        frmMain frmHelp = null;
        public NT37700_Gamma(frmMain NT37700)
        {
            frmHelp = NT37700;
            InitializeComponent();
            frmHelp.Child_window_hide_already = true;
            frmHelp.Child_Gamma_Window_Open_Enable = false;//防止重复开启子窗体            
        }
        //NT37700 监听卸载
        private void NT37700_Gamma_FormClosed(object sender, FormClosedEventArgs e)
        {
            ////frmHelp.comPort.Close()
            frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(NT37700C_DataReceived);
            frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
            ////frmHelp.comPort.Open();
            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "NT37700_Gamma_FormClosed！\r\n";
            frmHelp.Child_Gamma_Window_Open_Enable = true; //允许其他子窗体打开
            frmHelp.Child_window_hide_already = false;
            frmHelp.IC_comboBox.Enabled = true;
        }

        private void NT37700_Gamma_VisibleChanged(object sender, EventArgs e)
        {
            if (frmHelp.Child_window_hide_already == false) //子窗体已开启标识
            {
                ////frmHelp.comPort.Close()
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(NT37700C_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                ////frmHelp.comPort.Open();
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "NT37700_Gamma_FormHide！\r\n";
                frmHelp.Child_window_hide_already = true; //子窗体已隐藏标识
            }
            else
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(NT37700C_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "NT37700_Gamma_FormOpened！\r\n";
                frmHelp.Child_window_hide_already = false; //子窗体已开启标识
            }
        }
        //串口接收
        private void NT37700C_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            if (IClosing) return;
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = frmHelp.comPort.BytesToRead;
                byte[] buf = new byte[n];
                byte data;

                frmHelp.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    // 0x4E: NT37700C的Gamma读写、寄存器读写、OTP读写    设置  返回
                    if (buffer[0] == 0x4E && Setting.TempRecord.cmdLen > 0)
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, Setting.TempRecord.cmdLen);//正确分析一条数据，从缓存中移除数据。 
                            }
                        }
                    }
                    else
                    {
                        buffer.RemoveRange(0, buffer.Count);
                    }
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        frmHelp.T1.Enabled = false; //停止超时计数
                        if (Rec_tmp[0] == 0x4E)
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x03:
                                    {
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "GAMMA Update\t写 OK！\r\n";
                                    }
                                    break;
                                case 0x08:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "GAMMA \t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "GAMMA \t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x09:
                                    {
                                        if (Setting.TempRecord.NT37700CCmdType == 0x03)
                                        {
                                            NT37700C_R0.Value = Decimal.Parse((Rec_tmp[4] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            NT37700C_R1.Value = Decimal.Parse((Rec_tmp[6] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            NT37700C_R2.Value = Decimal.Parse((Rec_tmp[8] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                            NT37700C_R3.Value = Decimal.Parse((Rec_tmp[10] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            NT37700C_R4.Value = Decimal.Parse((Rec_tmp[12] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                            NT37700C_R5.Value = Decimal.Parse((Rec_tmp[14] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            NT37700C_R6.Value = Decimal.Parse((Rec_tmp[16] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            NT37700C_R7.Value = Decimal.Parse((Rec_tmp[18] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());

                                            NT37700C_R8.Value = Decimal.Parse((Rec_tmp[20] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                            NT37700C_R9.Value = Decimal.Parse((Rec_tmp[22] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                            NT37700C_R10.Value = Decimal.Parse((Rec_tmp[24] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                            NT37700C_R11.Value = Decimal.Parse((Rec_tmp[26] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                            NT37700C_R12.Value = Decimal.Parse((Rec_tmp[28] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            NT37700C_R13.Value = Decimal.Parse((Rec_tmp[30] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                            NT37700C_R14.Value = Decimal.Parse((Rec_tmp[32] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                            NT37700C_R15.Value = Decimal.Parse((Rec_tmp[34] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());

                                            NT37700C_R16.Value = Decimal.Parse((Rec_tmp[36] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                            NT37700C_R17.Value = Decimal.Parse((Rec_tmp[38] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                            NT37700C_R18.Value = Decimal.Parse((Rec_tmp[40] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                            NT37700C_R19.Value = Decimal.Parse((Rec_tmp[42] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                            NT37700C_R20.Value = Decimal.Parse((Rec_tmp[44] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                            NT37700C_R21.Value = Decimal.Parse((Rec_tmp[46] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                            NT37700C_R22.Value = Decimal.Parse((Rec_tmp[48] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                            NT37700C_R23.Value = Decimal.Parse((Rec_tmp[50] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());

                                            NT37700C_R24.Value = Decimal.Parse((Rec_tmp[52] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                            NT37700C_R25.Value = Decimal.Parse((Rec_tmp[54] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                            NT37700C_R26.Value = Decimal.Parse((Rec_tmp[56] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                            NT37700C_R27.Value = Decimal.Parse((Rec_tmp[58] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[59].ToString());
                                            NT37700C_R28.Value = Decimal.Parse((Rec_tmp[60] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[61].ToString());
                                            NT37700C_R29.Value = Decimal.Parse((Rec_tmp[62] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[63].ToString());
                                            NT37700C_R30.Value = Decimal.Parse((Rec_tmp[64] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[65].ToString());
                                            NT37700C_R31.Value = Decimal.Parse((Rec_tmp[66] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[67].ToString());
                                            NT37700C_R32.Value = Decimal.Parse((Rec_tmp[68] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[69].ToString());
                                            NT37700C_R33.Value = Decimal.Parse((Rec_tmp[70] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[71].ToString());
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.NT37700CCmdType == 0x04)
                                        {
                                            NT37700C_G0.Value = Decimal.Parse((Rec_tmp[4] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            NT37700C_G1.Value = Decimal.Parse((Rec_tmp[6] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            NT37700C_G2.Value = Decimal.Parse((Rec_tmp[8] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                            NT37700C_G3.Value = Decimal.Parse((Rec_tmp[10] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            NT37700C_G4.Value = Decimal.Parse((Rec_tmp[12] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                            NT37700C_G5.Value = Decimal.Parse((Rec_tmp[14] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            NT37700C_G6.Value = Decimal.Parse((Rec_tmp[16] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            NT37700C_G7.Value = Decimal.Parse((Rec_tmp[18] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());

                                            NT37700C_G8.Value = Decimal.Parse((Rec_tmp[20] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                            NT37700C_G9.Value = Decimal.Parse((Rec_tmp[22] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                            NT37700C_G10.Value = Decimal.Parse((Rec_tmp[24] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                            NT37700C_G11.Value = Decimal.Parse((Rec_tmp[26] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                            NT37700C_G12.Value = Decimal.Parse((Rec_tmp[28] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            NT37700C_G13.Value = Decimal.Parse((Rec_tmp[30] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                            NT37700C_G14.Value = Decimal.Parse((Rec_tmp[32] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                            NT37700C_G15.Value = Decimal.Parse((Rec_tmp[34] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());

                                            NT37700C_G16.Value = Decimal.Parse((Rec_tmp[36] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                            NT37700C_G17.Value = Decimal.Parse((Rec_tmp[38] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                            NT37700C_G18.Value = Decimal.Parse((Rec_tmp[40] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                            NT37700C_G19.Value = Decimal.Parse((Rec_tmp[42] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                            NT37700C_G20.Value = Decimal.Parse((Rec_tmp[44] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                            NT37700C_G21.Value = Decimal.Parse((Rec_tmp[46] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                            NT37700C_G22.Value = Decimal.Parse((Rec_tmp[48] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                            NT37700C_G23.Value = Decimal.Parse((Rec_tmp[50] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());

                                            NT37700C_G24.Value = Decimal.Parse((Rec_tmp[52] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                            NT37700C_G25.Value = Decimal.Parse((Rec_tmp[54] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                            NT37700C_G26.Value = Decimal.Parse((Rec_tmp[56] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                            NT37700C_G27.Value = Decimal.Parse((Rec_tmp[58] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[59].ToString());
                                            NT37700C_G28.Value = Decimal.Parse((Rec_tmp[60] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[61].ToString());
                                            NT37700C_G29.Value = Decimal.Parse((Rec_tmp[62] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[63].ToString());
                                            NT37700C_G30.Value = Decimal.Parse((Rec_tmp[64] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[65].ToString());
                                            NT37700C_G31.Value = Decimal.Parse((Rec_tmp[66] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[67].ToString());
                                            NT37700C_G32.Value = Decimal.Parse((Rec_tmp[68] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[69].ToString());
                                            NT37700C_G33.Value = Decimal.Parse((Rec_tmp[70] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[71].ToString());
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "绿 Gamma数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.NT37700CCmdType == 0x05)
                                        {
                                            NT37700C_B0.Value = Decimal.Parse((Rec_tmp[4] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            NT37700C_B1.Value = Decimal.Parse((Rec_tmp[6] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            NT37700C_B2.Value = Decimal.Parse((Rec_tmp[8] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                            NT37700C_B3.Value = Decimal.Parse((Rec_tmp[10] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            NT37700C_B4.Value = Decimal.Parse((Rec_tmp[12] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                            NT37700C_B5.Value = Decimal.Parse((Rec_tmp[14] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            NT37700C_B6.Value = Decimal.Parse((Rec_tmp[16] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            NT37700C_B7.Value = Decimal.Parse((Rec_tmp[18] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());

                                            NT37700C_B8.Value = Decimal.Parse((Rec_tmp[20] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                            NT37700C_B9.Value = Decimal.Parse((Rec_tmp[22] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                            NT37700C_B10.Value = Decimal.Parse((Rec_tmp[24] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                            NT37700C_B11.Value = Decimal.Parse((Rec_tmp[26] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                            NT37700C_B12.Value = Decimal.Parse((Rec_tmp[28] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            NT37700C_B13.Value = Decimal.Parse((Rec_tmp[30] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                            NT37700C_B14.Value = Decimal.Parse((Rec_tmp[32] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                            NT37700C_B15.Value = Decimal.Parse((Rec_tmp[34] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());

                                            NT37700C_B16.Value = Decimal.Parse((Rec_tmp[36] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                            NT37700C_B17.Value = Decimal.Parse((Rec_tmp[38] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                            NT37700C_B18.Value = Decimal.Parse((Rec_tmp[40] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                            NT37700C_B19.Value = Decimal.Parse((Rec_tmp[42] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                            NT37700C_B20.Value = Decimal.Parse((Rec_tmp[44] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                            NT37700C_B21.Value = Decimal.Parse((Rec_tmp[46] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                            NT37700C_B22.Value = Decimal.Parse((Rec_tmp[48] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                            NT37700C_B23.Value = Decimal.Parse((Rec_tmp[50] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());

                                            NT37700C_B24.Value = Decimal.Parse((Rec_tmp[52] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                            NT37700C_B25.Value = Decimal.Parse((Rec_tmp[54] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                            NT37700C_B26.Value = Decimal.Parse((Rec_tmp[56] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                            NT37700C_B27.Value = Decimal.Parse((Rec_tmp[58] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[59].ToString());
                                            NT37700C_B28.Value = Decimal.Parse((Rec_tmp[60] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[61].ToString());
                                            NT37700C_B29.Value = Decimal.Parse((Rec_tmp[62] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[63].ToString());
                                            NT37700C_B30.Value = Decimal.Parse((Rec_tmp[64] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[65].ToString());
                                            NT37700C_B31.Value = Decimal.Parse((Rec_tmp[66] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[67].ToString());
                                            NT37700C_B32.Value = Decimal.Parse((Rec_tmp[68] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[69].ToString());
                                            NT37700C_B33.Value = Decimal.Parse((Rec_tmp[70] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[71].ToString());
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "蓝 Gamma数据\t读 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x0D:
                                    {
                                        if (Rec_tmp[3] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP \t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP \t写 ERROR！\r\n";
                                    }
                                    break;

                                case 0x20:
                                    {
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "flash QE: " + Rec_tmp[3].ToString("x") + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "\r\n";
                                    }
                                    break;

                                case 0x21:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Erase Flash Finished \t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Erase Flash ERROR \t写 ERROR！\r\n";
                                    }
                                    break;

                                case 0x22:
                                    {
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Flash IC Status: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "\r\n";
                                    }
                                    break;

                                case 0x23:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Check Flash  \t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Check Flash  \t写 ERROR！\r\n";
                                    }
                                    break;

                                case 0x24:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Step Five  \t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Step Five  \t写 ERROR！\r\n";
                                    }
                                    break;

                                case 0x25:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP Five  \t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP Five  \t写 ERROR！\r\n";
                                    }
                                    break;
                                default: break;
                            }
                        }
                        frmHelp.textBox1.SelectionStart = this.frmHelp.textBox1.Text.Length;
                        frmHelp.textBox1.ScrollToCaret();//滚动到光标处
                        if (frmHelp.textBox1.Lines.Length > 50) frmHelp.textBox1.Text = null;
                    }));
                }
            }
            finally
            {
                Listening = false;
            }
        }
        //Gamma保存
        private void NT37700C_SaveGamma_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
            string str = "保存Gamma文件";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    str = str + "R0," + NT37700C_R0.Text + "," + "G0," + NT37700C_G0.Text + "," + "B0," + NT37700C_B0.Text + "\r\n";
                    str = str + "R1," + NT37700C_R1.Text + "," + "G1," + NT37700C_G1.Text + "," + "B1," + NT37700C_B1.Text + "\r\n";
                    str = str + "R3," + NT37700C_R2.Text + "," + "G3," + NT37700C_G2.Text + "," + "B3," + NT37700C_B2.Text + "\r\n";
                    str = str + "R5," + NT37700C_R3.Text + "," + "G5," + NT37700C_G3.Text + "," + "B5," + NT37700C_B3.Text + "\r\n";
                    str = str + "R7," + NT37700C_R4.Text + "," + "G7," + NT37700C_G4.Text + "," + "B7," + NT37700C_B4.Text + "\r\n";
                    str = str + "R11," + NT37700C_R5.Text + "," + "G11," + NT37700C_G5.Text + "," + "B11," + NT37700C_B5.Text + "\r\n";
                    str = str + "R15," + NT37700C_R6.Text + "," + "G15," + NT37700C_G6.Text + "," + "B15," + NT37700C_B6.Text + "\r\n";
                    str = str + "R19," + NT37700C_R7.Text + "," + "G19," + NT37700C_G7.Text + "," + "B20," + NT37700C_B7.Text + "\r\n";
                    str = str + "R23," + NT37700C_R8.Text + "," + "G23," + NT37700C_G8.Text + "," + "B23," + NT37700C_B8.Text + "\r\n";
                    str = str + "R27," + NT37700C_R9.Text + "," + "G27," + NT37700C_G9.Text + "," + "B27," + NT37700C_B9.Text + "\r\n";
                    str = str + "R31," + NT37700C_R10.Text + "," + "G31," + NT37700C_G10.Text + "," + "B31," + NT37700C_B10.Text + "\r\n";
                    str = str + "R35," + NT37700C_R11.Text + "," + "G35," + NT37700C_G11.Text + "," + "B35," + NT37700C_B11.Text + "\r\n";
                    str = str + "R39," + NT37700C_R12.Text + "," + "G39," + NT37700C_G12.Text + "," + "B39," + NT37700C_B12.Text + "\r\n";
                    str = str + "R47," + NT37700C_R13.Text + "," + "G47," + NT37700C_G13.Text + "," + "B47," + NT37700C_B13.Text + "\r\n";
                    str = str + "R55," + NT37700C_R14.Text + "," + "G55," + NT37700C_G14.Text + "," + "B55," + NT37700C_B14.Text + "\r\n";
                    str = str + "R63," + NT37700C_R15.Text + "," + "G63," + NT37700C_G15.Text + "," + "B63," + NT37700C_B15.Text + "\r\n";
                    str = str + "R71," + NT37700C_R16.Text + "," + "G71," + NT37700C_G16.Text + "," + "B71," + NT37700C_B16.Text + "\r\n";
                    str = str + "R79," + NT37700C_R17.Text + "," + "G79," + NT37700C_G17.Text + "," + "B79," + NT37700C_B17.Text + "\r\n";
                    str = str + "R95," + NT37700C_R18.Text + "," + "G95," + NT37700C_G18.Text + "," + "B95," + NT37700C_B18.Text + "\r\n";
                    str = str + "R111," + NT37700C_R19.Text + "," + "G111," + NT37700C_G19.Text + "," + "B111," + NT37700C_B19.Text + "\r\n";
                    str = str + "R127," + NT37700C_R20.Text + "," + "G127," + NT37700C_G20.Text + "," + "B127," + NT37700C_B20.Text + "\r\n";
                    str = str + "R159," + NT37700C_R21.Text + "," + "G159," + NT37700C_G21.Text + "," + "B159," + NT37700C_B21.Text + "\r\n";
                    str = str + "R191," + NT37700C_R22.Text + "," + "G191," + NT37700C_G22.Text + "," + "B191," + NT37700C_B22.Text + "\r\n";
                    str = str + "R223," + NT37700C_R23.Text + "," + "G223," + NT37700C_G23.Text + "," + "B223," + NT37700C_B23.Text + "\r\n";
                    str = str + "R255," + NT37700C_R24.Text + "," + "G255," + NT37700C_G24.Text + "," + "B255," + NT37700C_B24.Text + "\r\n";
                    str = str + "R127," + NT37700C_R25.Text + "," + "G127," + NT37700C_G25.Text + "," + "B127," + NT37700C_B25.Text + "\r\n";
                    str = str + "R159," + NT37700C_R26.Text + "," + "G159," + NT37700C_G26.Text + "," + "B159," + NT37700C_B26.Text + "\r\n";
                    str = str + "R191," + NT37700C_R27.Text + "," + "G191," + NT37700C_G27.Text + "," + "B191," + NT37700C_B27.Text + "\r\n";
                    str = str + "R223," + NT37700C_R28.Text + "," + "G223," + NT37700C_G28.Text + "," + "B223," + NT37700C_B28.Text + "\r\n";
                    str = str + "R255," + NT37700C_R29.Text + "," + "G255," + NT37700C_G29.Text + "," + "B255," + NT37700C_B29.Text + "\r\n";
                    str = str + "R127," + NT37700C_R30.Text + "," + "G127," + NT37700C_G30.Text + "," + "B127," + NT37700C_B30.Text + "\r\n";
                    str = str + "R159," + NT37700C_R31.Text + "," + "G159," + NT37700C_G31.Text + "," + "B159," + NT37700C_B31.Text + "\r\n";
                    str = str + "R191," + NT37700C_R32.Text + "," + "G191," + NT37700C_G32.Text + "," + "B191," + NT37700C_B32.Text + "\r\n";
                    str = str + "R223," + NT37700C_R33.Text + "," + "G223," + NT37700C_G33.Text + "," + "B223," + NT37700C_B33.Text + "\r\n";

                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        } //
        //Gamma载入
        private void NT37700C_LoadGamma_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            string strtmp = "";
            of.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; ;
            of.Title = "打开Gamma文件";

            if (of.ShowDialog() == DialogResult.OK)
            {
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = streamReader.ReadLine();
                //循环读取文本信息
                while (!string.IsNullOrEmpty(strLine))
                {
                    strtmp = strtmp + strLine + ",";
                    strLine = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();
                string[] arrtmp = strtmp.Split(new Char[] { ',' });
                NT37700C_R0.Text = arrtmp[1].ToString(); NT37700C_G0.Text = arrtmp[3].ToString(); NT37700C_B0.Text = arrtmp[5].ToString();
                NT37700C_R1.Text = arrtmp[7].ToString(); NT37700C_G1.Text = arrtmp[9].ToString(); NT37700C_B1.Text = arrtmp[11].ToString();
                NT37700C_R2.Text = arrtmp[13].ToString(); NT37700C_G2.Text = arrtmp[15].ToString(); NT37700C_B2.Text = arrtmp[17].ToString();
                NT37700C_R3.Text = arrtmp[19].ToString(); NT37700C_G3.Text = arrtmp[21].ToString(); NT37700C_B3.Text = arrtmp[23].ToString();
                NT37700C_R4.Text = arrtmp[25].ToString(); NT37700C_G4.Text = arrtmp[27].ToString(); NT37700C_B4.Text = arrtmp[29].ToString();
                NT37700C_R5.Text = arrtmp[31].ToString(); NT37700C_G5.Text = arrtmp[33].ToString(); NT37700C_B5.Text = arrtmp[35].ToString();
                NT37700C_R6.Text = arrtmp[37].ToString(); NT37700C_G6.Text = arrtmp[39].ToString(); NT37700C_B6.Text = arrtmp[41].ToString();
                NT37700C_R7.Text = arrtmp[43].ToString(); NT37700C_G7.Text = arrtmp[45].ToString(); NT37700C_B7.Text = arrtmp[47].ToString();
                NT37700C_R8.Text = arrtmp[49].ToString(); NT37700C_G8.Text = arrtmp[51].ToString(); NT37700C_B8.Text = arrtmp[53].ToString();
                NT37700C_R9.Text = arrtmp[55].ToString(); NT37700C_G9.Text = arrtmp[57].ToString(); NT37700C_B9.Text = arrtmp[59].ToString();
                NT37700C_R10.Text = arrtmp[61].ToString(); NT37700C_G10.Text = arrtmp[63].ToString(); NT37700C_B10.Text = arrtmp[65].ToString();
                NT37700C_R11.Text = arrtmp[67].ToString(); NT37700C_G11.Text = arrtmp[69].ToString(); NT37700C_B11.Text = arrtmp[71].ToString();
                NT37700C_R12.Text = arrtmp[73].ToString(); NT37700C_G12.Text = arrtmp[75].ToString(); NT37700C_B12.Text = arrtmp[77].ToString();
                NT37700C_R13.Text = arrtmp[79].ToString(); NT37700C_G13.Text = arrtmp[81].ToString(); NT37700C_B13.Text = arrtmp[83].ToString();
                NT37700C_R14.Text = arrtmp[85].ToString(); NT37700C_G14.Text = arrtmp[87].ToString(); NT37700C_B14.Text = arrtmp[89].ToString();
                NT37700C_R15.Text = arrtmp[91].ToString(); NT37700C_G15.Text = arrtmp[93].ToString(); NT37700C_B15.Text = arrtmp[95].ToString();
                NT37700C_R16.Text = arrtmp[97].ToString(); NT37700C_G16.Text = arrtmp[99].ToString(); NT37700C_B16.Text = arrtmp[101].ToString();
                NT37700C_R17.Text = arrtmp[103].ToString(); NT37700C_G17.Text = arrtmp[105].ToString(); NT37700C_B17.Text = arrtmp[107].ToString();
                NT37700C_R18.Text = arrtmp[109].ToString(); NT37700C_G18.Text = arrtmp[111].ToString(); NT37700C_B18.Text = arrtmp[113].ToString();
                NT37700C_R19.Text = arrtmp[115].ToString(); NT37700C_G19.Text = arrtmp[117].ToString(); NT37700C_B19.Text = arrtmp[119].ToString();
                NT37700C_R20.Text = arrtmp[121].ToString(); NT37700C_G20.Text = arrtmp[123].ToString(); NT37700C_B20.Text = arrtmp[125].ToString();
                NT37700C_R21.Text = arrtmp[127].ToString(); NT37700C_G21.Text = arrtmp[129].ToString(); NT37700C_B21.Text = arrtmp[131].ToString();
                NT37700C_R22.Text = arrtmp[133].ToString(); NT37700C_G22.Text = arrtmp[135].ToString(); NT37700C_B22.Text = arrtmp[137].ToString();
                NT37700C_R23.Text = arrtmp[139].ToString(); NT37700C_G23.Text = arrtmp[141].ToString(); NT37700C_B23.Text = arrtmp[143].ToString();
                NT37700C_R24.Text = arrtmp[145].ToString(); NT37700C_G24.Text = arrtmp[147].ToString(); NT37700C_B24.Text = arrtmp[149].ToString();
                NT37700C_R25.Text = arrtmp[151].ToString(); NT37700C_G25.Text = arrtmp[153].ToString(); NT37700C_B25.Text = arrtmp[155].ToString();
                NT37700C_R26.Text = arrtmp[157].ToString(); NT37700C_G26.Text = arrtmp[159].ToString(); NT37700C_B26.Text = arrtmp[161].ToString();
                NT37700C_R27.Text = arrtmp[163].ToString(); NT37700C_G27.Text = arrtmp[165].ToString(); NT37700C_B27.Text = arrtmp[167].ToString();
                NT37700C_R28.Text = arrtmp[169].ToString(); NT37700C_G28.Text = arrtmp[171].ToString(); NT37700C_B28.Text = arrtmp[173].ToString();
                NT37700C_R29.Text = arrtmp[175].ToString(); NT37700C_G29.Text = arrtmp[177].ToString(); NT37700C_B29.Text = arrtmp[179].ToString();
                NT37700C_R30.Text = arrtmp[181].ToString(); NT37700C_G30.Text = arrtmp[183].ToString(); NT37700C_B30.Text = arrtmp[185].ToString();
                NT37700C_R31.Text = arrtmp[187].ToString(); NT37700C_G31.Text = arrtmp[189].ToString(); NT37700C_B31.Text = arrtmp[191].ToString();
                NT37700C_R32.Text = arrtmp[193].ToString(); NT37700C_G32.Text = arrtmp[195].ToString(); NT37700C_B32.Text = arrtmp[197].ToString();
                NT37700C_R33.Text = arrtmp[199].ToString(); NT37700C_G33.Text = arrtmp[201].ToString(); NT37700C_B33.Text = arrtmp[203].ToString();
            }
        }

        //Gamma2.2目标值计算
        private void btnCalculateNT37700C_Click(object sender, EventArgs e)
        {
            double Lr_d, Lg_d, Lb_d;
            //Text_Processing(Gamma_SetBox.Text);//S提取
            string[] arr = Gamma_SetBox.Lines;
            {
                try
                {
                    Diven_Gamma.Clear();
                    Diven_Offset.Clear();
                    for (int i = 0; i < arr.Length; i++)
                    {
                        string line = arr[i];
                        line = line.Replace(" ", "");
                        if (line.Contains("TUNING_SPEC"))
                        {
                            line = line.Replace("TUNING_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr0 = line.Split(',');
                            Setting.Gamma_Property Gamma = new Setting.Gamma_Property(Convert.ToInt32(arr0[0]), Convert.ToInt32(arr0[1]), Convert.ToDouble(arr0[2]), Convert.ToDouble(arr0[3]), Convert.ToDouble(arr0[4]));
                            Diven_Gamma.Add(Gamma);
                        }
                        else if (line.Contains("LXY_SPEC"))
                        {
                            line = line.Replace("LXY_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr1 = line.Split(',');
                            //将Offset对象放入集合
                            Setting.Offset_Property Offset = new Setting.Offset_Property(Convert.ToInt32(arr1[0]), Convert.ToDouble(arr1[1]), Convert.ToDouble(arr1[2]), Convert.ToDouble(arr1[3]));
                            Diven_Offset.Add(Offset);
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("格式输入有误");
                }
            }

            try  //获取调整spec
            {
                txtNT_loff.Text = Diven_Offset[0].LUM_OFF.ToString();
                txtNT_xoff.Text = Diven_Offset[0].X_OFF.ToString();
                txtNT_yoff.Text = Diven_Offset[0].Y_OFF.ToString();

                int Total_points = Diven_Gamma[0].Points_num;
                D_lums = new double[Total_points, 3];
                D_lum = new double[Total_points];

                txtNT_gamma.Text = Diven_Gamma[0].Curve.ToString();
                txtNT_lum.Text = Diven_Gamma[0].Lum.ToString();
                txtNT_x.Text = Diven_Gamma[0].X.ToString();
                txtNT_y.Text = Diven_Gamma[0].Y.ToString();

                for (int p = 0; p < Total_points; p++)
                {   //Diven_Offset[p].NO 灰阶绑点                 
                    D_lum[p] = (Math.Pow((Diven_Offset[p].NO / 255.0), Diven_Gamma[0].Curve) * Diven_Gamma[0].Lum); //每绑点目标亮度
                }

                CaSDKObj.YUV_to_RGB(Diven_Gamma[0].Y, Diven_Gamma[0].X, out Lr_d, out Lb_d, out Lg_d);//R/B/G分亮度转换

                for (int i = 0; i < Total_points; i++)
                {
                    D_lums[i, 0] = D_lum[i] * Lr_d; //R/B/G分亮度计算
                    D_lums[i, 1] = D_lum[i] * Lg_d;
                    D_lums[i, 2] = D_lum[i] * Lb_d;
                }
            }
            catch
            {
                MessageBox.Show("格式输入有误");
            }

        }
        //自动调节启动开关
        private void btnAutoGammas_NT37700C_Click(object sender, EventArgs e)
        {
            index = 0;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;

            if (NT37700C_AOD_Inter_cbx.Checked == true)  //AOD gamma adjust in AOD mode or not
            {
                //R66451_btnPrevPic_Run("A" + Parttern_NT37700C[index].ToString() + Parttern_NT37700C[index].ToString() + Parttern_NT37700C[index].ToString() + ".bmp");
                frmHelp.numPatternR.Value = 255;
                frmHelp.numPatternG.Value = 255;
                frmHelp.numPatternB.Value = 255;
                frmHelp.XX0.Value = (int)(frmHelp.HACT.Value) * 20 / 100; frmHelp.XX1.Value = (int)(frmHelp.HACT.Value) * 80 / 100;//Partial display
                frmHelp.YY0.Value = (int)(frmHelp.VACT.Value) * 35 / 100; frmHelp.YY1.Value = (int)(frmHelp.VACT.Value) * 65 / 100;
                Application.DoEvents();
                frmHelp.OLED_Show_Click(frmHelp.btnlogic_pic, null);
            }
            else
            {
                frmHelp.numPatternR.Value = 255;
                frmHelp.numPatternG.Value = 255;
                frmHelp.numPatternB.Value = 255;
                Application.DoEvents();
                frmHelp.OLED_Show_Click(frmHelp.btnPatternW, e);
            }
            Thread.Sleep(10);
            T20.Enabled = true;

        }
        //自动调节关闭开关  
        private void NT37700C_Stop_Click(object sender, EventArgs e)
        {
            T20.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
        }
        private void NT37700_Gamma_Load(object sender, EventArgs e)
        {

        }

        private void T20_Tick(object sender, EventArgs e)
        {
            NT37700C_1D_gamma(null, null);
        }
        private void 配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!File.Exists(Path))
            {
                FileInfo myfile = new FileInfo(Path);
                FileStream fs = myfile.Create();
                fs.Close();
            }
            StreamWriter sw = File.AppendText(Path);
            sw.WriteLine(Gamma_SetBox.Text);
            sw.Flush();
            sw.Close();
            //System.Diagnostics.Process.Start(Path);//启动文本
        }

        private void Text_Processing(string txt_path)
        {
            using (StreamReader reader = new StreamReader(txt_path))
            {
                string line = reader.ReadLine();
                try
                {
                    while (line != "" && line != null)
                    {
                        line = line.Replace(" ", "");
                        if (line.Contains("TUNING_SPEC"))
                        {
                            line = line.Replace("TUNING_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr0 = line.Split(',');
                            Setting.Gamma_Property Gamma = new Setting.Gamma_Property(Convert.ToInt32(arr0[0]), Convert.ToInt32(arr0[1]), Convert.ToDouble(arr0[2]), Convert.ToDouble(arr0[3]), Convert.ToDouble(arr0[4]));
                            Diven_Gamma.Add(Gamma);
                        }
                        else if (line.Contains("LXY_SPEC"))
                        {
                            line = line.Replace("LXY_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr1 = line.Split(',');
                            //将Offset对象放入集合
                            Setting.Offset_Property Offset = new Setting.Offset_Property(Convert.ToInt32(arr1[0]), Convert.ToDouble(arr1[1]), Convert.ToDouble(arr1[2]), Convert.ToDouble(arr1[3]));
                            Diven_Offset.Add(Offset);
                        }

                        line = reader.ReadLine();
                    }
                }
                catch
                {
                    MessageBox.Show("格式输入有误");
                }
            }
        }

        private void NT37700C_Others_Click(object sender, EventArgs e)
        {
            NT37700C_GAMMA_Click(NT37700C_GammaW_Update, null);
            Thread.Sleep(50);

            Send_buf = new byte[7];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 7;

            Send_buf[0] = 0x69;
            Send_buf[2] = 0x02;

            switch (btnTmp.Name)
            {
                case "NT37700C_OTP": //otp Gamma
                    {
                        Send_buf[1] = 0x0D;
                        if (NT37700C_OTP_ALL.Checked == true)
                        { Send_buf[3] = 0xA0; }
                        else if (NT37700C_OTP_P0.Checked == true)
                        { Send_buf[3] = 0x00; }
                        else if (NT37700C_OTP_P1.Checked == true)
                        { Send_buf[3] = 0x01; }
                        else if (NT37700C_OTP_P2.Checked == true)
                        { Send_buf[3] = 0x02; }
                        else if (NT37700C_OTP_P3.Checked == true)
                        { Send_buf[3] = 0x03; }
                        else if (NT37700C_OTP_P4.Checked == true)
                        { Send_buf[3] = 0x04; }
                        else if (NT37700C_OTP_P5.Checked == true)
                        { Send_buf[3] = 0x05; }
                        else if (NT37700C_OTP_P6.Checked == true)
                        { Send_buf[3] = 0x06; }
                        else if (NT37700C_OTP_P7.Checked == true)
                        { Send_buf[3] = 0x07; }
                        else if (NT37700C_OTP_P8.Checked == true)
                        { Send_buf[3] = 0x08; }
                    }
                    break;
                case "NT37700C_QE1":
                    {
                        Send_buf[1] = 0x20;
                    }
                    break;
                case "NT37700C_Erase_Flash":
                    {
                        Send_buf[1] = 0x21;
                    }
                    break;
                case "NT37700C_Program":
                    {
                        Send_buf[1] = 0x22;
                    }
                    break;
                case "NT37700C_Show":
                    {
                        Send_buf[1] = 0x23;
                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 7);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        private void NT37700C_GAMMA_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[77];
            Setting.TempRecord.cmdLen = 77;
            Send_buf[2] = 72;
            Send_buf[3] = 0;
            Send_buf[0] = 0x4E; //NT37700

            switch (btnTmp.Name)
            {
                case "NT37700C_GammaR_RED":
                    {
                        Send_buf[1] = 0x09;                             //读Gamma
                        Send_buf[3] = 0x00;                            //红色red
                        Send_buf[4] = (byte)(NT37700C_Gamma_Group.Value);
                        Setting.TempRecord.NT37700CCmdType = 0x03;
                    }
                    break;
                case "NT37700C_GammaR_GREEN":
                    {
                        Send_buf[1] = 0x09;                         //  读Gamma
                        Send_buf[3] = 0x42;                        //绿色 green
                        Send_buf[4] = (byte)(NT37700C_Gamma_Group.Value);
                        Setting.TempRecord.NT37700CCmdType = 0x04;
                    }
                    break;
                case "NT37700C_GammaR_BLUE":
                    {
                        Send_buf[1] = 0x09;                         //   读Gamma
                        Send_buf[3] = 0x81;                        //   blue  
                        Send_buf[4] = (byte)(NT37700C_Gamma_Group.Value);
                        Setting.TempRecord.NT37700CCmdType = 0x05;
                    }
                    break;
                case "NT37700C_GammaW_RED":
                    {
                        Send_buf[1] = 0x08;                       //写Gamma
                        Send_buf[3] = 0x00;                       //红色1
                        Send_buf[4] = (byte)(NT37700C_Gamma_Group.Value);//gamma_group_select

                        Send_buf[5] = Convert.ToByte(Convert.ToUInt16(NT37700C_R0.Value.ToString()) >> 8);
                        Send_buf[6] = Convert.ToByte((Convert.ToUInt16(NT37700C_R0.Value.ToString())) & 0xff);
                        Send_buf[7] = Convert.ToByte(Convert.ToUInt16(NT37700C_R1.Value.ToString()) >> 8);
                        Send_buf[8] = Convert.ToByte((Convert.ToUInt16(NT37700C_R1.Value.ToString())) & 0xff);
                        Send_buf[9] = Convert.ToByte(Convert.ToUInt16(NT37700C_R2.Value.ToString()) >> 8);
                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(NT37700C_R2.Value.ToString())) & 0xff);
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(NT37700C_R3.Value.ToString()) >> 8);
                        Send_buf[12] = Convert.ToByte((Convert.ToUInt16(NT37700C_R3.Value.ToString())) & 0xff);
                        Send_buf[13] = Convert.ToByte(Convert.ToUInt16(NT37700C_R4.Value.ToString()) >> 8);
                        Send_buf[14] = Convert.ToByte((Convert.ToUInt16(NT37700C_R4.Value.ToString())) & 0xff);
                        Send_buf[15] = Convert.ToByte(Convert.ToUInt16(NT37700C_R5.Value.ToString()) >> 8);
                        Send_buf[16] = Convert.ToByte((Convert.ToUInt16(NT37700C_R5.Value.ToString())) & 0xff);
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(NT37700C_R6.Value.ToString()) >> 8);
                        Send_buf[18] = Convert.ToByte((Convert.ToUInt16(NT37700C_R6.Value.ToString())) & 0xff);
                        Send_buf[19] = Convert.ToByte(Convert.ToUInt16(NT37700C_R7.Value.ToString()) >> 8);
                        Send_buf[20] = Convert.ToByte((Convert.ToUInt16(NT37700C_R7.Value.ToString())) & 0xff);
                        Send_buf[21] = Convert.ToByte(Convert.ToUInt16(NT37700C_R8.Value.ToString()) >> 8);
                        Send_buf[22] = Convert.ToByte((Convert.ToUInt16(NT37700C_R8.Value.ToString())) & 0xff);
                        Send_buf[23] = Convert.ToByte(Convert.ToUInt16(NT37700C_R9.Value.ToString()) >> 8);
                        Send_buf[24] = Convert.ToByte((Convert.ToUInt16(NT37700C_R9.Value.ToString())) & 0xff);
                        Send_buf[25] = Convert.ToByte(Convert.ToUInt16(NT37700C_R10.Value.ToString()) >> 8);
                        Send_buf[26] = Convert.ToByte((Convert.ToUInt16(NT37700C_R10.Value.ToString())) & 0xff);
                        Send_buf[27] = Convert.ToByte(Convert.ToUInt16(NT37700C_R11.Value.ToString()) >> 8);
                        Send_buf[28] = Convert.ToByte((Convert.ToUInt16(NT37700C_R11.Value.ToString())) & 0xff);

                        Send_buf[29] = Convert.ToByte(Convert.ToUInt16(NT37700C_R12.Value.ToString()) >> 8);
                        Send_buf[30] = Convert.ToByte((Convert.ToUInt16(NT37700C_R12.Value.ToString())) & 0xff);
                        Send_buf[31] = Convert.ToByte(Convert.ToUInt16(NT37700C_R13.Value.ToString()) >> 8);
                        Send_buf[32] = Convert.ToByte((Convert.ToUInt16(NT37700C_R13.Value.ToString())) & 0xff);
                        Send_buf[33] = Convert.ToByte(Convert.ToUInt16(NT37700C_R14.Value.ToString()) >> 8);
                        Send_buf[34] = Convert.ToByte((Convert.ToUInt16(NT37700C_R14.Value.ToString())) & 0xff);
                        Send_buf[35] = Convert.ToByte(Convert.ToUInt16(NT37700C_R15.Value.ToString()) >> 8);
                        Send_buf[36] = Convert.ToByte((Convert.ToUInt16(NT37700C_R15.Value.ToString())) & 0xff);
                        Send_buf[37] = Convert.ToByte(Convert.ToUInt16(NT37700C_R16.Value.ToString()) >> 8);
                        Send_buf[38] = Convert.ToByte((Convert.ToUInt16(NT37700C_R16.Value.ToString())) & 0xff);
                        Send_buf[39] = Convert.ToByte(Convert.ToUInt16(NT37700C_R17.Value.ToString()) >> 8);
                        Send_buf[40] = Convert.ToByte((Convert.ToUInt16(NT37700C_R17.Value.ToString())) & 0xff);
                        Send_buf[41] = Convert.ToByte(Convert.ToUInt16(NT37700C_R18.Value.ToString()) >> 8);
                        Send_buf[42] = Convert.ToByte((Convert.ToUInt16(NT37700C_R18.Value.ToString())) & 0xff);
                        Send_buf[43] = Convert.ToByte(Convert.ToUInt16(NT37700C_R19.Value.ToString()) >> 8);
                        Send_buf[44] = Convert.ToByte((Convert.ToUInt16(NT37700C_R19.Value.ToString())) & 0xff);
                        Send_buf[45] = Convert.ToByte(Convert.ToUInt16(NT37700C_R20.Value.ToString()) >> 8);
                        Send_buf[46] = Convert.ToByte((Convert.ToUInt16(NT37700C_R20.Value.ToString())) & 0xff);
                        Send_buf[47] = Convert.ToByte(Convert.ToUInt16(NT37700C_R21.Value.ToString()) >> 8);
                        Send_buf[48] = Convert.ToByte((Convert.ToUInt16(NT37700C_R21.Value.ToString())) & 0xff);
                        Send_buf[49] = Convert.ToByte(Convert.ToUInt16(NT37700C_R22.Value.ToString()) >> 8);
                        Send_buf[50] = Convert.ToByte((Convert.ToUInt16(NT37700C_R22.Value.ToString())) & 0xff);
                        Send_buf[51] = Convert.ToByte(Convert.ToUInt16(NT37700C_R23.Value.ToString()) >> 8);
                        Send_buf[52] = Convert.ToByte((Convert.ToUInt16(NT37700C_R23.Value.ToString())) & 0xff);

                        Send_buf[53] = Convert.ToByte(Convert.ToUInt16(NT37700C_R24.Value.ToString()) >> 8);
                        Send_buf[54] = Convert.ToByte((Convert.ToUInt16(NT37700C_R24.Value.ToString())) & 0xff);
                        Send_buf[55] = Convert.ToByte(Convert.ToUInt16(NT37700C_R25.Value.ToString()) >> 8);
                        Send_buf[56] = Convert.ToByte((Convert.ToUInt16(NT37700C_R25.Value.ToString())) & 0xff);
                        Send_buf[57] = Convert.ToByte(Convert.ToUInt16(NT37700C_R26.Value.ToString()) >> 8);
                        Send_buf[58] = Convert.ToByte((Convert.ToUInt16(NT37700C_R26.Value.ToString())) & 0xff);
                        Send_buf[59] = Convert.ToByte(Convert.ToUInt16(NT37700C_R27.Value.ToString()) >> 8);
                        Send_buf[60] = Convert.ToByte((Convert.ToUInt16(NT37700C_R27.Value.ToString())) & 0xff);
                        Send_buf[61] = Convert.ToByte(Convert.ToUInt16(NT37700C_R28.Value.ToString()) >> 8);
                        Send_buf[62] = Convert.ToByte((Convert.ToUInt16(NT37700C_R28.Value.ToString())) & 0xff);
                        Send_buf[63] = Convert.ToByte(Convert.ToUInt16(NT37700C_R29.Value.ToString()) >> 8);
                        Send_buf[64] = Convert.ToByte((Convert.ToUInt16(NT37700C_R29.Value.ToString())) & 0xff);
                        Send_buf[65] = Convert.ToByte(Convert.ToUInt16(NT37700C_R30.Value.ToString()) >> 8);
                        Send_buf[66] = Convert.ToByte((Convert.ToUInt16(NT37700C_R30.Value.ToString())) & 0xff);
                        Send_buf[67] = Convert.ToByte(Convert.ToUInt16(NT37700C_R31.Value.ToString()) >> 8);
                        Send_buf[68] = Convert.ToByte((Convert.ToUInt16(NT37700C_R31.Value.ToString())) & 0xff);
                        Send_buf[69] = Convert.ToByte(Convert.ToUInt16(NT37700C_R32.Value.ToString()) >> 8);
                        Send_buf[70] = Convert.ToByte((Convert.ToUInt16(NT37700C_R32.Value.ToString())) & 0xff);
                        Send_buf[71] = Convert.ToByte(Convert.ToUInt16(NT37700C_R33.Value.ToString()) >> 8);
                        Send_buf[72] = Convert.ToByte((Convert.ToUInt16(NT37700C_R33.Value.ToString())) & 0xff);
                        Send_buf[73] = Convert.ToByte(Convert.ToUInt16(NT37700C_R33.Value.ToString()) >> 8);
                        Send_buf[74] = Convert.ToByte((Convert.ToUInt16(NT37700C_R33.Value.ToString())) & 0xff);
                    }
                    break;
                case "NT37700C_GammaW_GREEN":
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] = 0x42;                       //绿色1
                        Send_buf[4] = (byte)(NT37700C_Gamma_Group.Value);

                        Send_buf[5] = Convert.ToByte(Convert.ToUInt16(NT37700C_G0.Value.ToString()) >> 8);
                        Send_buf[6] = Convert.ToByte((Convert.ToUInt16(NT37700C_G0.Value.ToString())) & 0xff);
                        Send_buf[7] = Convert.ToByte(Convert.ToUInt16(NT37700C_G1.Value.ToString()) >> 8);
                        Send_buf[8] = Convert.ToByte((Convert.ToUInt16(NT37700C_G1.Value.ToString())) & 0xff);
                        Send_buf[9] = Convert.ToByte(Convert.ToUInt16(NT37700C_G2.Value.ToString()) >> 8);
                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(NT37700C_G2.Value.ToString())) & 0xff);
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(NT37700C_G3.Value.ToString()) >> 8);
                        Send_buf[12] = Convert.ToByte((Convert.ToUInt16(NT37700C_G3.Value.ToString())) & 0xff);
                        Send_buf[13] = Convert.ToByte(Convert.ToUInt16(NT37700C_G4.Value.ToString()) >> 8);
                        Send_buf[14] = Convert.ToByte((Convert.ToUInt16(NT37700C_G4.Value.ToString())) & 0xff);
                        Send_buf[15] = Convert.ToByte(Convert.ToUInt16(NT37700C_G5.Value.ToString()) >> 8);
                        Send_buf[16] = Convert.ToByte((Convert.ToUInt16(NT37700C_G5.Value.ToString())) & 0xff);
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(NT37700C_G6.Value.ToString()) >> 8);
                        Send_buf[18] = Convert.ToByte((Convert.ToUInt16(NT37700C_G6.Value.ToString())) & 0xff);
                        Send_buf[19] = Convert.ToByte(Convert.ToUInt16(NT37700C_G7.Value.ToString()) >> 8);
                        Send_buf[20] = Convert.ToByte((Convert.ToUInt16(NT37700C_G7.Value.ToString())) & 0xff);
                        Send_buf[21] = Convert.ToByte(Convert.ToUInt16(NT37700C_G8.Value.ToString()) >> 8);
                        Send_buf[22] = Convert.ToByte((Convert.ToUInt16(NT37700C_G8.Value.ToString())) & 0xff);
                        Send_buf[23] = Convert.ToByte(Convert.ToUInt16(NT37700C_G9.Value.ToString()) >> 8);
                        Send_buf[24] = Convert.ToByte((Convert.ToUInt16(NT37700C_G9.Value.ToString())) & 0xff);
                        Send_buf[25] = Convert.ToByte(Convert.ToUInt16(NT37700C_G10.Value.ToString()) >> 8);
                        Send_buf[26] = Convert.ToByte((Convert.ToUInt16(NT37700C_G10.Value.ToString())) & 0xff);
                        Send_buf[27] = Convert.ToByte(Convert.ToUInt16(NT37700C_G11.Value.ToString()) >> 8);
                        Send_buf[28] = Convert.ToByte((Convert.ToUInt16(NT37700C_G11.Value.ToString())) & 0xff);

                        Send_buf[29] = Convert.ToByte(Convert.ToUInt16(NT37700C_G12.Value.ToString()) >> 8);
                        Send_buf[30] = Convert.ToByte((Convert.ToUInt16(NT37700C_G12.Value.ToString())) & 0xff);
                        Send_buf[31] = Convert.ToByte(Convert.ToUInt16(NT37700C_G13.Value.ToString()) >> 8);
                        Send_buf[32] = Convert.ToByte((Convert.ToUInt16(NT37700C_G13.Value.ToString())) & 0xff);
                        Send_buf[33] = Convert.ToByte(Convert.ToUInt16(NT37700C_G14.Value.ToString()) >> 8);
                        Send_buf[34] = Convert.ToByte((Convert.ToUInt16(NT37700C_G14.Value.ToString())) & 0xff);
                        Send_buf[35] = Convert.ToByte(Convert.ToUInt16(NT37700C_G15.Value.ToString()) >> 8);
                        Send_buf[36] = Convert.ToByte((Convert.ToUInt16(NT37700C_G15.Value.ToString())) & 0xff);
                        Send_buf[37] = Convert.ToByte(Convert.ToUInt16(NT37700C_G16.Value.ToString()) >> 8);
                        Send_buf[38] = Convert.ToByte((Convert.ToUInt16(NT37700C_G16.Value.ToString())) & 0xff);
                        Send_buf[39] = Convert.ToByte(Convert.ToUInt16(NT37700C_G17.Value.ToString()) >> 8);
                        Send_buf[40] = Convert.ToByte((Convert.ToUInt16(NT37700C_G17.Value.ToString())) & 0xff);
                        Send_buf[41] = Convert.ToByte(Convert.ToUInt16(NT37700C_G18.Value.ToString()) >> 8);
                        Send_buf[42] = Convert.ToByte((Convert.ToUInt16(NT37700C_G18.Value.ToString())) & 0xff);
                        Send_buf[43] = Convert.ToByte(Convert.ToUInt16(NT37700C_G19.Value.ToString()) >> 8);
                        Send_buf[44] = Convert.ToByte((Convert.ToUInt16(NT37700C_G19.Value.ToString())) & 0xff);
                        Send_buf[45] = Convert.ToByte(Convert.ToUInt16(NT37700C_G20.Value.ToString()) >> 8);
                        Send_buf[46] = Convert.ToByte((Convert.ToUInt16(NT37700C_G20.Value.ToString())) & 0xff);
                        Send_buf[47] = Convert.ToByte(Convert.ToUInt16(NT37700C_G21.Value.ToString()) >> 8);
                        Send_buf[48] = Convert.ToByte((Convert.ToUInt16(NT37700C_G21.Value.ToString())) & 0xff);
                        Send_buf[49] = Convert.ToByte(Convert.ToUInt16(NT37700C_G22.Value.ToString()) >> 8);
                        Send_buf[50] = Convert.ToByte((Convert.ToUInt16(NT37700C_G22.Value.ToString())) & 0xff);
                        Send_buf[51] = Convert.ToByte(Convert.ToUInt16(NT37700C_G23.Value.ToString()) >> 8);
                        Send_buf[52] = Convert.ToByte((Convert.ToUInt16(NT37700C_G23.Value.ToString())) & 0xff);

                        Send_buf[53] = Convert.ToByte(Convert.ToUInt16(NT37700C_G24.Value.ToString()) >> 8);
                        Send_buf[54] = Convert.ToByte((Convert.ToUInt16(NT37700C_G24.Value.ToString())) & 0xff);
                        Send_buf[55] = Convert.ToByte(Convert.ToUInt16(NT37700C_G25.Value.ToString()) >> 8);
                        Send_buf[56] = Convert.ToByte((Convert.ToUInt16(NT37700C_G25.Value.ToString())) & 0xff);
                        Send_buf[57] = Convert.ToByte(Convert.ToUInt16(NT37700C_G26.Value.ToString()) >> 8);
                        Send_buf[58] = Convert.ToByte((Convert.ToUInt16(NT37700C_G26.Value.ToString())) & 0xff);
                        Send_buf[59] = Convert.ToByte(Convert.ToUInt16(NT37700C_G27.Value.ToString()) >> 8);
                        Send_buf[60] = Convert.ToByte((Convert.ToUInt16(NT37700C_G27.Value.ToString())) & 0xff);
                        Send_buf[61] = Convert.ToByte(Convert.ToUInt16(NT37700C_G28.Value.ToString()) >> 8);
                        Send_buf[62] = Convert.ToByte((Convert.ToUInt16(NT37700C_G28.Value.ToString())) & 0xff);
                        Send_buf[63] = Convert.ToByte(Convert.ToUInt16(NT37700C_G29.Value.ToString()) >> 8);
                        Send_buf[64] = Convert.ToByte((Convert.ToUInt16(NT37700C_G29.Value.ToString())) & 0xff);
                        Send_buf[65] = Convert.ToByte(Convert.ToUInt16(NT37700C_G30.Value.ToString()) >> 8);
                        Send_buf[66] = Convert.ToByte((Convert.ToUInt16(NT37700C_G30.Value.ToString())) & 0xff);
                        Send_buf[67] = Convert.ToByte(Convert.ToUInt16(NT37700C_G31.Value.ToString()) >> 8);
                        Send_buf[68] = Convert.ToByte((Convert.ToUInt16(NT37700C_G31.Value.ToString())) & 0xff);
                        Send_buf[69] = Convert.ToByte(Convert.ToUInt16(NT37700C_G32.Value.ToString()) >> 8);
                        Send_buf[70] = Convert.ToByte((Convert.ToUInt16(NT37700C_G32.Value.ToString())) & 0xff);
                        Send_buf[71] = Convert.ToByte(Convert.ToUInt16(NT37700C_G33.Value.ToString()) >> 8);
                        Send_buf[72] = Convert.ToByte((Convert.ToUInt16(NT37700C_G33.Value.ToString())) & 0xff);
                        Send_buf[73] = Convert.ToByte(Convert.ToUInt16(NT37700C_G33.Value.ToString()) >> 8);
                        Send_buf[74] = Convert.ToByte((Convert.ToUInt16(NT37700C_G33.Value.ToString())) & 0xff);
                    }
                    break;
                case "NT37700C_GammaW_BLUE":     //8
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] = 0x81;
                        Send_buf[4] = (byte)(NT37700C_Gamma_Group.Value);

                        Send_buf[5] = Convert.ToByte(Convert.ToUInt16(NT37700C_B0.Value.ToString()) >> 8);
                        Send_buf[6] = Convert.ToByte((Convert.ToUInt16(NT37700C_B0.Value.ToString())) & 0xff);
                        Send_buf[7] = Convert.ToByte(Convert.ToUInt16(NT37700C_B1.Value.ToString()) >> 8);
                        Send_buf[8] = Convert.ToByte((Convert.ToUInt16(NT37700C_B1.Value.ToString())) & 0xff);
                        Send_buf[9] = Convert.ToByte(Convert.ToUInt16(NT37700C_B2.Value.ToString()) >> 8);
                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(NT37700C_B2.Value.ToString())) & 0xff);
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(NT37700C_B3.Value.ToString()) >> 8);
                        Send_buf[12] = Convert.ToByte((Convert.ToUInt16(NT37700C_B3.Value.ToString())) & 0xff);
                        Send_buf[13] = Convert.ToByte(Convert.ToUInt16(NT37700C_B4.Value.ToString()) >> 8);
                        Send_buf[14] = Convert.ToByte((Convert.ToUInt16(NT37700C_B4.Value.ToString())) & 0xff);
                        Send_buf[15] = Convert.ToByte(Convert.ToUInt16(NT37700C_B5.Value.ToString()) >> 8);
                        Send_buf[16] = Convert.ToByte((Convert.ToUInt16(NT37700C_B5.Value.ToString())) & 0xff);
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(NT37700C_B6.Value.ToString()) >> 8);
                        Send_buf[18] = Convert.ToByte((Convert.ToUInt16(NT37700C_B6.Value.ToString())) & 0xff);
                        Send_buf[19] = Convert.ToByte(Convert.ToUInt16(NT37700C_B7.Value.ToString()) >> 8);
                        Send_buf[20] = Convert.ToByte((Convert.ToUInt16(NT37700C_B7.Value.ToString())) & 0xff);
                        Send_buf[21] = Convert.ToByte(Convert.ToUInt16(NT37700C_B8.Value.ToString()) >> 8);
                        Send_buf[22] = Convert.ToByte((Convert.ToUInt16(NT37700C_B8.Value.ToString())) & 0xff);
                        Send_buf[23] = Convert.ToByte(Convert.ToUInt16(NT37700C_B9.Value.ToString()) >> 8);
                        Send_buf[24] = Convert.ToByte((Convert.ToUInt16(NT37700C_B9.Value.ToString())) & 0xff);
                        Send_buf[25] = Convert.ToByte(Convert.ToUInt16(NT37700C_B10.Value.ToString()) >> 8);
                        Send_buf[26] = Convert.ToByte((Convert.ToUInt16(NT37700C_B10.Value.ToString())) & 0xff);
                        Send_buf[27] = Convert.ToByte(Convert.ToUInt16(NT37700C_B11.Value.ToString()) >> 8);
                        Send_buf[28] = Convert.ToByte((Convert.ToUInt16(NT37700C_B11.Value.ToString())) & 0xff);

                        Send_buf[29] = Convert.ToByte(Convert.ToUInt16(NT37700C_B12.Value.ToString()) >> 8);
                        Send_buf[30] = Convert.ToByte((Convert.ToUInt16(NT37700C_B12.Value.ToString())) & 0xff);
                        Send_buf[31] = Convert.ToByte(Convert.ToUInt16(NT37700C_B13.Value.ToString()) >> 8);
                        Send_buf[32] = Convert.ToByte((Convert.ToUInt16(NT37700C_B13.Value.ToString())) & 0xff);
                        Send_buf[33] = Convert.ToByte(Convert.ToUInt16(NT37700C_B14.Value.ToString()) >> 8);
                        Send_buf[34] = Convert.ToByte((Convert.ToUInt16(NT37700C_B14.Value.ToString())) & 0xff);
                        Send_buf[35] = Convert.ToByte(Convert.ToUInt16(NT37700C_B15.Value.ToString()) >> 8);
                        Send_buf[36] = Convert.ToByte((Convert.ToUInt16(NT37700C_B15.Value.ToString())) & 0xff);
                        Send_buf[37] = Convert.ToByte(Convert.ToUInt16(NT37700C_B16.Value.ToString()) >> 8);
                        Send_buf[38] = Convert.ToByte((Convert.ToUInt16(NT37700C_B16.Value.ToString())) & 0xff);
                        Send_buf[39] = Convert.ToByte(Convert.ToUInt16(NT37700C_B17.Value.ToString()) >> 8);
                        Send_buf[40] = Convert.ToByte((Convert.ToUInt16(NT37700C_B17.Value.ToString())) & 0xff);
                        Send_buf[41] = Convert.ToByte(Convert.ToUInt16(NT37700C_B18.Value.ToString()) >> 8);
                        Send_buf[42] = Convert.ToByte((Convert.ToUInt16(NT37700C_B18.Value.ToString())) & 0xff);
                        Send_buf[43] = Convert.ToByte(Convert.ToUInt16(NT37700C_B19.Value.ToString()) >> 8);
                        Send_buf[44] = Convert.ToByte((Convert.ToUInt16(NT37700C_B19.Value.ToString())) & 0xff);
                        Send_buf[45] = Convert.ToByte(Convert.ToUInt16(NT37700C_B20.Value.ToString()) >> 8);
                        Send_buf[46] = Convert.ToByte((Convert.ToUInt16(NT37700C_B20.Value.ToString())) & 0xff);
                        Send_buf[47] = Convert.ToByte(Convert.ToUInt16(NT37700C_B21.Value.ToString()) >> 8);
                        Send_buf[48] = Convert.ToByte((Convert.ToUInt16(NT37700C_B21.Value.ToString())) & 0xff);
                        Send_buf[49] = Convert.ToByte(Convert.ToUInt16(NT37700C_B22.Value.ToString()) >> 8);
                        Send_buf[50] = Convert.ToByte((Convert.ToUInt16(NT37700C_B22.Value.ToString())) & 0xff);
                        Send_buf[51] = Convert.ToByte(Convert.ToUInt16(NT37700C_B23.Value.ToString()) >> 8);
                        Send_buf[52] = Convert.ToByte((Convert.ToUInt16(NT37700C_B23.Value.ToString())) & 0xff);

                        Send_buf[53] = Convert.ToByte(Convert.ToUInt16(NT37700C_B24.Value.ToString()) >> 8);
                        Send_buf[54] = Convert.ToByte((Convert.ToUInt16(NT37700C_B24.Value.ToString())) & 0xff);
                        Send_buf[55] = Convert.ToByte(Convert.ToUInt16(NT37700C_B25.Value.ToString()) >> 8);
                        Send_buf[56] = Convert.ToByte((Convert.ToUInt16(NT37700C_B25.Value.ToString())) & 0xff);
                        Send_buf[57] = Convert.ToByte(Convert.ToUInt16(NT37700C_B26.Value.ToString()) >> 8);
                        Send_buf[58] = Convert.ToByte((Convert.ToUInt16(NT37700C_B26.Value.ToString())) & 0xff);
                        Send_buf[59] = Convert.ToByte(Convert.ToUInt16(NT37700C_B27.Value.ToString()) >> 8);
                        Send_buf[60] = Convert.ToByte((Convert.ToUInt16(NT37700C_B27.Value.ToString())) & 0xff);
                        Send_buf[61] = Convert.ToByte(Convert.ToUInt16(NT37700C_B28.Value.ToString()) >> 8);
                        Send_buf[62] = Convert.ToByte((Convert.ToUInt16(NT37700C_B28.Value.ToString())) & 0xff);
                        Send_buf[63] = Convert.ToByte(Convert.ToUInt16(NT37700C_B29.Value.ToString()) >> 8);
                        Send_buf[64] = Convert.ToByte((Convert.ToUInt16(NT37700C_B29.Value.ToString())) & 0xff);
                        Send_buf[65] = Convert.ToByte(Convert.ToUInt16(NT37700C_B30.Value.ToString()) >> 8);
                        Send_buf[66] = Convert.ToByte((Convert.ToUInt16(NT37700C_B30.Value.ToString())) & 0xff);
                        Send_buf[67] = Convert.ToByte(Convert.ToUInt16(NT37700C_B31.Value.ToString()) >> 8);
                        Send_buf[68] = Convert.ToByte((Convert.ToUInt16(NT37700C_B31.Value.ToString())) & 0xff);
                        Send_buf[69] = Convert.ToByte(Convert.ToUInt16(NT37700C_B32.Value.ToString()) >> 8);
                        Send_buf[70] = Convert.ToByte((Convert.ToUInt16(NT37700C_B32.Value.ToString())) & 0xff);
                        Send_buf[71] = Convert.ToByte(Convert.ToUInt16(NT37700C_B33.Value.ToString()) >> 8);
                        Send_buf[72] = Convert.ToByte((Convert.ToUInt16(NT37700C_B33.Value.ToString())) & 0xff);
                        Send_buf[73] = Convert.ToByte(Convert.ToUInt16(NT37700C_B33.Value.ToString()) >> 8);
                        Send_buf[74] = Convert.ToByte((Convert.ToUInt16(NT37700C_B33.Value.ToString())) & 0xff);
                    }
                    break;
                case "NT37700C_GammaW_Update":
                    {
                        Send_buf[1] = 0x03;                       //update Gamma
                        Send_buf[3] = 0x00;                       //红色1
                        Send_buf[4] = (byte)(NT37700C_Gamma_Group.Value);//gamma_group_select
                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[75], out Send_buf[76]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 77);   //NT37700C
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }


        private void NT37700C_1D_gamma(Object myObject, EventArgs e)
        {
            int total_points = 33;
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;

            frmHelp.Msr_CASDK();

            if (Gamma_adjust_timeout_flag == true)//如果是0绑点
            {
                flag1 = 0;
                flag2 = 0;
                flag3 = 0;
            }
            else
            {
                if ((frmHelp.CASDK_LUM < (D_lum[index] + D_lum[index] * Diven_Offset[index].LUM_OFF / 100)) && (frmHelp.CASDK_LUM > (D_lum[index] - D_lum[index] * Diven_Offset[index].LUM_OFF / 100)))//G
                { flag1 = 0; }
                else
                { flag1 = 1; }
                if ((frmHelp.CASDK_x < (Diven_Gamma[0].X + Diven_Offset[index].X_OFF)) && (frmHelp.CASDK_x > (Diven_Gamma[0].X - Diven_Offset[index].X_OFF)) && flag1 == 0)//R
                { flag3 = 0; }
                else
                { flag3 = 1; }
                if ((frmHelp.CASDK_y < (Diven_Gamma[0].Y + Diven_Offset[index].Y_OFF)) && (frmHelp.CASDK_y > (Diven_Gamma[0].Y - Diven_Offset[index].Y_OFF)) && flag1 == 0 && flag3 == 0)//B
                { flag2 = 0; }
                else
                { flag2 = 1; }

            }
            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                frmHelp.Gamma_auto_adjust_count = 0;
                //--------
                index++;

                if (index == total_points)
                {
                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "NT37700C gamma调试完毕!\r\n";
                }
                if (index < total_points)
                {
                    frmHelp.numPatternR.Value = Parttern_NT37700C[index];//固定255灰阶
                    frmHelp.numPatternG.Value = Parttern_NT37700C[index];
                    frmHelp.numPatternB.Value = Parttern_NT37700C[index];

                    if (NT37700C_AOD_Inter_cbx.Checked == true)  //AOD gamma adjust in AOD mode or not
                    {
                        frmHelp.XX0.Value = (int)(frmHelp.HACT.Value) * 20 / 100; frmHelp.XX1.Value = (int)(frmHelp.HACT.Value) * 80 / 100;//Partial display
                        frmHelp.YY0.Value = (int)(frmHelp.VACT.Value) * 35 / 100; frmHelp.YY1.Value = (int)(frmHelp.VACT.Value) * 65 / 100;
                        Application.DoEvents();
                        frmHelp.OLED_Show_Click(frmHelp.btnlogic_pic, null);
                    }
                    else
                    {
                        Application.DoEvents();
                        frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                        Thread.Sleep(100); //1秒=1000毫秒，
                        frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                    }
                    Thread.Sleep(10); //1秒=1000毫秒，
                    T20.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else
            {
                decimal r = 0, g = 0, b = 0;
                int flexible_gamma_count = 70;

                CaSDKObj.YUV_to_RGB(frmHelp.CASDK_y, frmHelp.CASDK_x, out Lr_t, out Lb_t, out Lg_t);

                lumr = frmHelp.CASDK_LUM * Lr_t;
                lumg = frmHelp.CASDK_LUM * Lg_t;
                lumb = frmHelp.CASDK_LUM * Lb_t;

                try
                {
                    switch (index)
                    {
                        case 0:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R33.Value, NT37700C_G33.Value, NT37700C_B33.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R33.Value = r;
                                NT37700C_G33.Value = g;
                                NT37700C_B33.Value = b;

                                if (NT37700C_G33.Value <= NT37700C_G32.Value)
                                {
                                    NT37700C_R32.Value = r - 0x10; NT37700C_G32.Value = g - 0x10; NT37700C_B32.Value = b - 0x10;
                                }
                                // auto_gamma_write(gamma_r[index], Convert.ToUInt16(RM692AX_R32.Value), gamma_g[index], Convert.ToUInt16(RM692AX_G32.Value), gamma_b[index], Convert.ToUInt16(RM692AX_B32.Value));
                            }
                            break;
                        case 1:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R32.Value, NT37700C_G32.Value, NT37700C_B32.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R32.Value = r;
                                NT37700C_G32.Value = g;
                                NT37700C_B32.Value = b;

                                if (NT37700C_G32.Value <= NT37700C_G31.Value)
                                {
                                    NT37700C_R31.Value = r - 0x10; NT37700C_G31.Value = g - 0x10; NT37700C_B31.Value = b - 0x10;
                                }
                            }
                            break;
                        case 2:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R31.Value, NT37700C_G31.Value, NT37700C_B31.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R31.Value = r;
                                NT37700C_G31.Value = g;
                                NT37700C_B31.Value = b;

                                if (NT37700C_G31.Value <= NT37700C_G30.Value)
                                {
                                    NT37700C_R30.Value = r - 0x10; NT37700C_G30.Value = g - 0x10; NT37700C_B30.Value = b - 0x10;
                                }
                            }
                            break;

                        case 3:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R30.Value, NT37700C_G30.Value, NT37700C_B30.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R30.Value = r;
                                NT37700C_G30.Value = g;
                                NT37700C_B30.Value = b;

                                if (NT37700C_G30.Value <= NT37700C_G29.Value)
                                {
                                    NT37700C_R29.Value = r - 0x10; NT37700C_G29.Value = g - 0x10; NT37700C_B29.Value = b - 0x10;
                                }
                            }
                            break;

                        case 4:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R29.Value, NT37700C_G29.Value, NT37700C_B29.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R29.Value = r;
                                NT37700C_G29.Value = g;
                                NT37700C_B29.Value = b;

                                if (NT37700C_G29.Value <= NT37700C_G28.Value)
                                {
                                    NT37700C_R28.Value = r - 0x10; NT37700C_G28.Value = g - 0x10; NT37700C_B28.Value = b - 0x10;
                                }
                            }
                            break;

                        case 5:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R28.Value, NT37700C_G28.Value, NT37700C_B28.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R28.Value = r;
                                NT37700C_G28.Value = g;
                                NT37700C_B28.Value = b;

                                if (NT37700C_G28.Value <= NT37700C_G27.Value)
                                {
                                    NT37700C_R27.Value = r - 0x10; NT37700C_G27.Value = g - 0x10; NT37700C_B27.Value = b - 0x10;
                                }
                            }
                            break;

                        case 6:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R27.Value, NT37700C_G27.Value, NT37700C_B27.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R27.Value = r;
                                NT37700C_G27.Value = g;
                                NT37700C_B27.Value = b;

                                if (NT37700C_G27.Value <= NT37700C_G26.Value)
                                {
                                    NT37700C_R26.Value = r - 0x10; NT37700C_G26.Value = g - 0x10; NT37700C_B26.Value = b - 0x10;
                                }
                            }
                            break;

                        case 7:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R26.Value, NT37700C_G26.Value, NT37700C_B26.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R26.Value = r;
                                NT37700C_G26.Value = g;
                                NT37700C_B26.Value = b;

                                if (NT37700C_G26.Value <= NT37700C_G25.Value)
                                {
                                    NT37700C_R25.Value = r - 0x10; NT37700C_G25.Value = g - 0x10; NT37700C_B25.Value = b - 0x10;
                                }
                            }
                            break;

                        case 8:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R25.Value, NT37700C_G25.Value, NT37700C_B25.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R25.Value = r;
                                NT37700C_G25.Value = g;
                                NT37700C_B25.Value = b;

                                if (NT37700C_G25.Value <= NT37700C_G24.Value)
                                {
                                    NT37700C_R24.Value = r - 0x10; NT37700C_G24.Value = g - 0x10; NT37700C_B24.Value = b - 0x10;
                                }
                            }
                            break;

                        case 9:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R24.Value, NT37700C_G24.Value, NT37700C_B24.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R24.Value = r;
                                NT37700C_G24.Value = g;
                                NT37700C_B24.Value = b;

                                if (NT37700C_G24.Value <= NT37700C_G23.Value)
                                {
                                    NT37700C_R23.Value = r - 0x10; NT37700C_G23.Value = g - 0x10; NT37700C_B23.Value = b - 0x10;
                                }
                            }
                            break;

                        case 10:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R23.Value, NT37700C_G23.Value, NT37700C_B23.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R23.Value = r;
                                NT37700C_G23.Value = g;
                                NT37700C_B23.Value = b;

                                if (NT37700C_G23.Value <= NT37700C_G22.Value)
                                {
                                    NT37700C_R22.Value = r - 0x10; NT37700C_G22.Value = g - 0x10; NT37700C_B22.Value = b - 0x10;
                                }
                            }
                            break;

                        case 11:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R22.Value, NT37700C_G22.Value, NT37700C_B22.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R22.Value = r;
                                NT37700C_G22.Value = g;
                                NT37700C_B22.Value = b;

                                if (NT37700C_G22.Value <= NT37700C_G21.Value)
                                {
                                    NT37700C_R21.Value = r - 0x10; NT37700C_G21.Value = g - 0x10; NT37700C_B21.Value = b - 0x10;
                                }
                            }
                            break;

                        case 12:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R21.Value, NT37700C_G21.Value, NT37700C_B21.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R21.Value = r;
                                NT37700C_G21.Value = g;
                                NT37700C_B21.Value = b;

                                if (NT37700C_G21.Value <= NT37700C_G20.Value)
                                {
                                    NT37700C_R20.Value = r - 0x10; NT37700C_G20.Value = g - 0x10; NT37700C_B20.Value = b - 0x10;
                                }
                            }
                            break;
                        case 13:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R20.Value, NT37700C_G20.Value, NT37700C_B20.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R20.Value = r;
                                NT37700C_G20.Value = g;
                                NT37700C_B20.Value = b;

                                if (NT37700C_G20.Value <= NT37700C_G19.Value)
                                {
                                    NT37700C_R19.Value = r - 0x10; NT37700C_G19.Value = g - 0x10; NT37700C_B19.Value = b - 0x10;
                                }
                            }
                            break;
                        case 14:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R19.Value, NT37700C_G19.Value, NT37700C_B19.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R19.Value = r;
                                NT37700C_G19.Value = g;
                                NT37700C_B19.Value = b;

                                if (NT37700C_G19.Value <= NT37700C_G18.Value)
                                {
                                    NT37700C_R18.Value = r - 0x10; NT37700C_G18.Value = g - 0x10; NT37700C_B18.Value = b - 0x10;
                                }
                            }
                            break;
                        case 15:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R18.Value, NT37700C_G18.Value, NT37700C_B18.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R18.Value = r;
                                NT37700C_G18.Value = g;
                                NT37700C_B18.Value = b;
                                if (NT37700C_G18.Value <= NT37700C_G17.Value)
                                {
                                    NT37700C_R17.Value = r - 0x10; NT37700C_G17.Value = g - 0x10; NT37700C_B17.Value = b - 0x10;
                                }
                            }
                            break;
                        case 16:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R17.Value, NT37700C_G17.Value, NT37700C_B17.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R17.Value = r;
                                NT37700C_G17.Value = g;
                                NT37700C_B17.Value = b;

                                if (NT37700C_G17.Value <= NT37700C_G16.Value)
                                {
                                    NT37700C_R16.Value = r - 0x10; NT37700C_G16.Value = g - 0x10; NT37700C_B16.Value = b - 0x10;
                                }
                            }
                            break;
                        case 17:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R16.Value, NT37700C_G16.Value, NT37700C_B16.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R16.Value = r;
                                NT37700C_G16.Value = g;
                                NT37700C_B16.Value = b;

                                if (NT37700C_G16.Value <= NT37700C_G15.Value)
                                {
                                    NT37700C_R15.Value = r - 0x10; NT37700C_G15.Value = g - 0x10; NT37700C_B15.Value = b - 0x10;
                                }
                            }
                            break;
                        case 18:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R15.Value, NT37700C_G15.Value, NT37700C_B15.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R15.Value = r;
                                NT37700C_G15.Value = g;
                                NT37700C_B15.Value = b;

                                if (NT37700C_G15.Value <= NT37700C_G14.Value)
                                {
                                    NT37700C_R14.Value = r - 0x10; NT37700C_G14.Value = g - 0x10; NT37700C_B14.Value = b - 0x10;
                                }
                            }
                            break;
                        case 19:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R14.Value, NT37700C_G14.Value, NT37700C_B14.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R14.Value = r;
                                NT37700C_G14.Value = g;
                                NT37700C_B14.Value = b;

                                if (NT37700C_G14.Value <= NT37700C_G13.Value)
                                {
                                    NT37700C_R13.Value = r - 0x10; NT37700C_G13.Value = g - 0x10; NT37700C_B13.Value = b - 0x10;
                                }
                            }
                            break;
                        case 20:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R13.Value, NT37700C_G13.Value, NT37700C_B13.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R13.Value = r;
                                NT37700C_G13.Value = g;
                                NT37700C_B13.Value = b;

                                if (NT37700C_G13.Value <= NT37700C_G12.Value)
                                {
                                    NT37700C_R12.Value = r - 0x10; NT37700C_G12.Value = g - 0x10; NT37700C_B12.Value = b - 0x10;
                                }
                            }
                            break;
                        case 21:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R12.Value, NT37700C_G12.Value, NT37700C_B12.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R12.Value = r;
                                NT37700C_G12.Value = g;
                                NT37700C_B12.Value = b;

                                if (NT37700C_G12.Value <= NT37700C_G11.Value)
                                {
                                    NT37700C_R11.Value = r - 0x10; NT37700C_G11.Value = g - 0x10; NT37700C_B11.Value = b - 0x10;
                                }
                            }
                            break;
                        case 22:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R11.Value, NT37700C_G11.Value, NT37700C_B11.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R11.Value = r;
                                NT37700C_G11.Value = g;
                                NT37700C_B11.Value = b;

                                if (NT37700C_G11.Value <= NT37700C_G10.Value)
                                {
                                    NT37700C_R10.Value = r - 0x10; NT37700C_G10.Value = g - 0x10; NT37700C_B10.Value = b - 0x10;
                                }
                            }
                            break;
                        case 23:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R10.Value, NT37700C_G10.Value, NT37700C_B10.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R10.Value = r;
                                NT37700C_G10.Value = g;
                                NT37700C_B10.Value = b;

                                if (NT37700C_G10.Value <= NT37700C_G9.Value)
                                {
                                    NT37700C_R9.Value = r - 0x10; NT37700C_G9.Value = g - 0x10; NT37700C_B9.Value = b - 0x10;
                                }
                            }
                            break;
                        case 24:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R9.Value, NT37700C_G9.Value, NT37700C_B9.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R9.Value = r;
                                NT37700C_G9.Value = g;
                                NT37700C_B9.Value = b;

                                if (NT37700C_G9.Value <= NT37700C_G8.Value)
                                {
                                    NT37700C_R8.Value = r - 0x10; NT37700C_G8.Value = g - 0x10; NT37700C_B8.Value = b - 0x10;
                                }
                            }
                            break;
                        case 25:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R8.Value, NT37700C_G8.Value, NT37700C_B8.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R8.Value = r;
                                NT37700C_G8.Value = g;
                                NT37700C_B8.Value = b;

                                if (NT37700C_G8.Value <= NT37700C_G7.Value)
                                {
                                    NT37700C_R7.Value = r - 0x10; NT37700C_G7.Value = g - 0x10; NT37700C_B7.Value = b - 0x10;
                                }
                            }
                            break;
                        case 26:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R7.Value, NT37700C_G7.Value, NT37700C_B7.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R7.Value = r;
                                NT37700C_G7.Value = g;
                                NT37700C_B7.Value = b;

                                if (NT37700C_G7.Value <= NT37700C_G6.Value)
                                {
                                    NT37700C_R6.Value = r - 0x10; NT37700C_G6.Value = g - 0x10; NT37700C_B6.Value = b - 0x10;
                                }
                            }
                            break;
                        case 27:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R6.Value, NT37700C_G6.Value, NT37700C_B6.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R6.Value = r;
                                NT37700C_G6.Value = g;
                                NT37700C_B6.Value = b;

                                if (NT37700C_G6.Value <= NT37700C_G5.Value)
                                {
                                    NT37700C_R5.Value = r - 0x10; NT37700C_G5.Value = g - 0x10; NT37700C_B5.Value = b - 0x10;
                                }
                            }
                            break;
                        case 28:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R5.Value, NT37700C_G5.Value, NT37700C_B5.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R5.Value = r;
                                NT37700C_G5.Value = g;
                                NT37700C_B5.Value = b;

                                if (NT37700C_G5.Value <= NT37700C_G4.Value)
                                {
                                    NT37700C_R4.Value = r - 0x10; NT37700C_G4.Value = g - 0x10; NT37700C_B4.Value = b - 0x10;
                                }
                            }
                            break;
                        case 29:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R4.Value, NT37700C_G4.Value, NT37700C_B4.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R4.Value = r;
                                NT37700C_G4.Value = g;
                                NT37700C_B4.Value = b;

                                if (NT37700C_G4.Value <= NT37700C_G3.Value)
                                {
                                    NT37700C_R3.Value = r - 10; NT37700C_G3.Value = g - 10; NT37700C_B3.Value = b - 10;
                                }
                            }
                            break;
                        case 30:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R3.Value, NT37700C_G3.Value, NT37700C_B3.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R3.Value = r;
                                NT37700C_G3.Value = g;
                                NT37700C_B3.Value = b;

                                if (NT37700C_G3.Value <= NT37700C_G2.Value)
                                {
                                    NT37700C_R2.Value = r - 5; NT37700C_G2.Value = g - 5; NT37700C_B2.Value = b - 5;
                                }
                            }
                            break;
                        case 31:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT37700C_R2.Value, NT37700C_G2.Value, NT37700C_B2.Value, Parttern_NT37700C[index], 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R2.Value = r;
                                NT37700C_G2.Value = g;
                                NT37700C_B2.Value = b;

                                if (NT37700C_G2.Value <= NT37700C_G1.Value)
                                {
                                    NT37700C_R1.Value = r - 5; NT37700C_G1.Value = g - 5; NT37700C_B1.Value = b - 5;
                                }
                            }
                            break;
                        case 32:
                            {
                                Gamma_adjust_timeout_flag = true;
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R1.Value = NT37700C_R2.Value - 0x10;
                                NT37700C_G1.Value = NT37700C_G2.Value - 0x10;
                                NT37700C_B1.Value = NT37700C_B2.Value - 0x10;
                            }
                            break;
                        case 33:
                            {
                                Gamma_adjust_timeout_flag = true;
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                try
                                {
                                    NT37700C_R0.Value = NT37700C_R1.Value - 0xfe;
                                }
                                catch
                                {
                                    NT37700C_R0.Value = 0;
                                }
                                try
                                {
                                    NT37700C_G0.Value = NT37700C_G1.Value - 0xfe;
                                }
                                catch
                                {
                                    NT37700C_G0.Value = 0;
                                }
                                try
                                {
                                    NT37700C_B0.Value = NT37700C_B1.Value - 0xfe;
                                }
                                catch
                                {
                                    NT37700C_B0.Value = 0;
                                }
                            }
                            break;
                        default:
                            break;
                    }
                    //   if (index < 13)
                    //   {
                    //Target_VTDR6100_Lum[index] = auto_lum;
                    //Target_VTDR6100_X[index] = auto_x;
                    //Target_VTDR6100_Y[index] = auto_y;
                    //  }
                    NT37700C_GAMMA_Click(NT37700C_GammaW_RED, null);
                    Thread.Sleep(20);
                    NT37700C_GAMMA_Click(NT37700C_GammaW_GREEN, null);
                    Thread.Sleep(20);
                    NT37700C_GAMMA_Click(NT37700C_GammaW_BLUE, null);
                }
                catch
                {
                    T20.Enabled = false;
                    MessageBox.Show("调整数值超限");
                }
            }
        }

        private void Gamma_SetBox_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
